package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    //用于添加套餐
    @PostMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息：{}", setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("新增套餐成功");
    }

    //用于套餐展示
    @GetMapping("/page")
    public R pageWithDish(Integer page, Integer pageSize, String name) {
        R r = setmealService.pageWithDish(page, pageSize, name);
        return r;
    }

    //用于删除套餐
    @DeleteMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R delete(@RequestParam List<Long> ids) {
        log.info("要删除的ids为{}", ids.toString());
        for (Long id : ids) {
            //查询套餐是否为售卖状态
            Setmeal sm = setmealService.getById(id);
            if (sm.getStatus() == 1) {
                return R.error("该套餐不为停售状态,请勿删除!");
            }
            //删除套餐
            setmealService.removeById(id);
            //删除套餐下对应商品数据
            LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
            qw.eq(SetmealDish::getSetmealId, sm.getId());
            setmealDishService.remove(qw);
            return R.success("删除成功!");
        }
        return R.error("删除失败,请稍后再试!");
    }

    //用于停用启用
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmeal",allEntries = true)
    public R updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        log.info("要修改的id为{}", ids.toString());
        for (Long id : ids) {
            LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Setmeal::getId, id).set(Setmeal::getStatus, status);
            setmealService.update(null, updateWrapper);
        }
        return R.success("操作成功");
    }

    //用于查询套餐
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        log.info("id{}", id);
        R bySId = setmealService.getBySId(id);
        return bySId;
    }

    //用于修改套餐
    @PutMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R updateWithDish(@RequestBody SetmealDto setmealDto){
        R r = setmealService.updateWithDish(setmealDto);
        return r;
    }

    //用于查询分类
    @GetMapping("/list")
    @Cacheable(value = "setmeal",key = "#setmeal.categoryId+':'+#setmeal.status")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
